package maple.thelittleredbook.logging.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import maple.thelittleredbook.common.dto.ApiResponse;
import maple.thelittleredbook.common.service.AuthService;
import maple.thelittleredbook.logging.dto.SysLogHistoryResponse;
import maple.thelittleredbook.logging.entity.SysLog;
import maple.thelittleredbook.logging.service.AdminSysLogService;
import maple.thelittleredbook.logging.annotation.SystemLog;

/**
 * 系统日志控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/sys-logs")
public class SysLogController {

    private final AdminSysLogService adminSysLogService;
    private final AuthService authService;

    @Autowired
    public SysLogController(AdminSysLogService adminSysLogService, AuthService authService) {
        this.adminSysLogService = adminSysLogService;
        this.authService = authService;
    }

    /**
     * 获取系统日志历史
     */
    @GetMapping
    @SystemLog(operation = "查询系统日志历史")
    public ResponseEntity<ApiResponse<SysLogHistoryResponse>> getSysLogHistory(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "size", required = false) Integer size,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "requestMethod", required = false) String requestMethod,
            @RequestParam(value = "operation", required = false) String operation,
            @RequestParam(value = "errorOnly", required = false) Boolean errorOnly) {

        // 获取当前用户信息
        Long currentUserId = authService.getCurrentUserId();
        if (currentUserId == null) {
            return ResponseEntity.ok(ApiResponse.fail("用户未登录或登录已过期"));
        }

        // 权限验证：只有管理员可以查看系统日志
        if (!authService.isCurrentUserAdmin()) {
            return ResponseEntity.ok(ApiResponse.fail("权限不足，只有管理员可以查看系统日志"));
        }

        log.info("管理员查询系统日志历史，管理员ID: {}, 查询用户ID: {}, 请求方法: {}, 操作类型: {}, 页码: {}, 大小: {}, 仅错误: {}",
                currentUserId, userId, requestMethod, operation, page, size, errorOnly);

        SysLogHistoryResponse response = adminSysLogService.getSysLogHistory(
                page, size, userId, requestMethod, operation, errorOnly);

        return ResponseEntity.ok(ApiResponse.success("查询成功", response));
    }

    /**
     * 获取系统日志详情
     */
    @GetMapping("/{logId}")
    @SystemLog(operation = "查询系统日志详情")
    public ResponseEntity<ApiResponse<SysLog>> getSysLogDetail(@PathVariable Long logId) {
        // 获取当前用户信息
        Long currentUserId = authService.getCurrentUserId();
        if (currentUserId == null) {
            return ResponseEntity.ok(ApiResponse.fail("用户未登录或登录已过期"));
        }

        // 权限验证：只有管理员可以查看系统日志
        if (!authService.isCurrentUserAdmin()) {
            return ResponseEntity.ok(ApiResponse.fail("权限不足，只有管理员可以查看系统日志"));
        }

        log.info("管理员查询系统日志详情，管理员ID: {}, 日志ID: {}", currentUserId, logId);

        SysLog sysLog = adminSysLogService.getSysLogById(logId);
        return ResponseEntity.ok(ApiResponse.success("查询成功", sysLog));
    }
}